const net = require('net');
const port = 12012;
const hostname = '127.0.0.1';

// 定义两个变量， 一个用来计数，一个用来保存客户端
let clients: any = {};
let clientName = 0;

// 创建服务器
const server = new net.createServer();

server.on('connection', (client: any) => {
    client.name = ++clientName; // 给每一个client起个名
    clients[client.name] = client; // 将client保存在clients

    client.on('data', function (msg: any) { //接收client发来的信息
        console.log(`客户端${client.name}发来一个信息：${msg}`);
    });
    client.write('abcd');

    client.on('error', function (e: any) { //监听客户端异常
        console.log('client error' + e);
        client.end();
    });

    client.on('close', function () {
        delete clients[client.name];
        console.log(`客户端${client.name}下线了`);
    });

});

server.listen(port, hostname, function () {
    console.log(`服务器运行在：${hostname}:${port}`);
});
